#include "financequerystandarditemmodel.h"

FinanceQueryStandardItemModel::FinanceQueryStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header1<<"时间"
          <<"单号"
         <<"操作员"
        <<"类型"
       <<"客户/供应商名称"
      <<"联系人"
     <<"电话"
    <<"金额"
    <<"备注";

    header2<<"时间"
          <<"单号"
         <<"操作员"
        <<"类型"
       <<"金额"
      <<"备注";

    header = header1;

    type = 0;
    setHorizontalHeaderLabels(header);
}

void FinanceQueryStandardItemModel::setType(int type){
    this->type = type;
    header = type==0?header1:header2;
}

void FinanceQueryStandardItemModel::reset(){
    clear();
    messages.clear();
    setHorizontalHeaderLabels(header);
}

void FinanceQueryStandardItemModel::insertMessage(QList<Message*> messages){
    for(Message *message:messages){
        insertMessage(*message);
    }
}

void FinanceQueryStandardItemModel::insertMessage(Message &message){
    QList<QStandardItem*> rowData;

    UserRelation *relation = message.getUserRelation();
    rowData.append(new QStandardItem(message.date));
    rowData.append(new QStandardItem(message.messageId));
    rowData.append(new QStandardItem(message.opNickName));
    rowData.append(new QStandardItem(message.getMessageTypeStr()));
    if(type==0){
        rowData.append(new QStandardItem(relation==nullptr?"":relation->nickName));
        rowData.append(new QStandardItem(relation==nullptr?"":relation->contact));
        rowData.append(new QStandardItem(relation==nullptr?"":relation->tel));
    }

    rowData.append(new QStandardItem(QString::number(message.getMoneyValue(), 'f', 2)));
    rowData.append(new QStandardItem(message.remark));

    messages.append(&message);
    appendRow(rowData);
}
